package cn.autumnorange.app.common.dto;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Objects;

@Setter
@Getter
public abstract class BaseCodeDto extends BaseStatusDto {
  private String code;

  public ResponseEntity checkCode(String errorParamName) {
    if (StringUtils.isBlank(code)) {

      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
          .body(ResultDto.createMessageResult(errorParamName + "不能为空"));
    }
    if (!StringUtils.isNumeric(code)) {
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
          .body(ResultDto.createMessageResult(errorParamName + "必须为正整数"));
    }
    if (Long.valueOf(code) < 1) {
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
          .body(ResultDto.createMessageResult(errorParamName + "必须为大于1的正整数"));
    }
    return null;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof BaseCodeDto)) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }
    BaseCodeDto that = (BaseCodeDto) o;
    return Objects.equals(code, that.code);
  }

  @Override
  public int hashCode() {

    return Objects.hash(super.hashCode(), code);
  }
}
